import requests
from bs4 import BeautifulSoup
import re
import os.path

user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36'
headers = {'User-Agent': user_agent}

session = requests.session()
page = session.get("http://weheartit.com/inspirations/beach", headers=headers)
soup = BeautifulSoup(page.text, 'lxml')  # 这里没有装lxml的话,把它去掉用默认的就好

# 匹配带有class属性的tr标签
taglist = soup.find_all('tr', attrs={'class': re.compile("(odd)|()")})
for trtag in taglist:
    tdlist = trtag.find_all('td')  # 在每个tr标签下,查找所有的td标签
    print(tdlist[1].string)   # 这里提取IP值
    print(tdlist[2].string)  # 这里提取端口值